<?php

namespace App\Trip\Orders\BookOrders\Plane\Passengers;

use App\Models\Plane\PlaneOrderPassengers\PlaneOrderPassenger;
use App\Trip\Orders\BookOrders\Params\AbstractOrderParam\AbstractOrderParam;
use App\Trip\Orders\BookOrders\Plane\Order\ChangeNewPlaneOrder;
use App\Trip\Orders\TripOrderState;
use App\Trip\Passengers\Passengers;
use App\Trip\Plane\Interfaces\PlaneQueryInterface;
use App\Trip\Plane\Interfaces\Result\ChangeFlightInfoInterface;
use Exception;

class ChangePlaneOrderPassengers
{
    public ChangeNewPlaneOrder  $orderInfo;
    private array               $passengers;
    private AbstractOrderParam  $order_param;
    private PlaneQueryInterface $service;

    /**
     * @throws Exception
     */
    public function __construct(ChangeNewPlaneOrder $orderInfo)
    {
        $this->orderInfo   = $orderInfo;
        $this->service     = $this->orderInfo->getOriginOrderInfo()->QueryStrategy();
        $this->passengers  = $this->orderInfo->getPassengerParam()->getParamPassengers();
        $this->order_param = $orderInfo->getOrderParam();
    }

    /**
     * @throws Exception
     */
    public function buildPassengers(): array
    {

        $change_key        = $this->order_param->change_key;
        $change_seat_item  = $this->service->getChangeSeatItem($change_key);
        $passengers        = $this->passengers;
        $origin_passengers = $this->orderInfo->getOriginOrderInfo()->getCanChangePassengers();
        /**
         * @var PlaneOrderPassenger $origin_passenger
         */
        foreach ($passengers as $pk => $passenger) {
            $passenger_type = $passenger['passenger_type'];
            // 如果是改签订单 验证乘客是否和原乘客匹配
            $origin_passenger = collect($origin_passengers)
                ->where('passenger_id', $passenger['passenger_id'])
                ->where('sequence', $passenger['sequence'])
                ->firstWhere('passenger_type', $passenger_type);
            // 验证改签用户是否匹配
            if (!$origin_passenger) {
                throw new Exception("乘客信息错误", 404);
            }
            // 验证改签用户
            $passengers[$pk]['origin_passenger'] = $origin_passenger;
            $passengers                          = $this->getPassengersCommonData($change_seat_item, $passengers, $pk, $passenger_type);
            // 出票后才有
            $passengers[$pk]['origin_ticket_number']    = $origin_passenger["ticket_number"];
            $passengers[$pk]['origin_sub_out_order_id'] = $origin_passenger["sub_out_order_id"];
            $passengers[$pk]['origin_cabin']            = $origin_passenger["cabin"];
            $passengers[$pk]['origin_fuel_fee']         = $origin_passenger["fuel_fee"];
            $passengers[$pk]['origin_construct_fee']    = $origin_passenger["construct_fee"];
            $passengers[$pk]['order_state']             = TripOrderState::ORDER_STATE_CHANGE_WAITING_PAY;
        }
        return $passengers;
    }

    /**
     * @throws Exception
     */
    function getPassengersCommonData(ChangeFlightInfoInterface $change_seat_item, mixed $passengers, int|string $pk, int $passenger_type): mixed
    {
        $passenger_type_map = [
            Passengers::PASSENGER_TYPE_ADULT => 'adult',
            Passengers::PASSENGER_TYPE_CHILD => 'child',
        ];
        $origin_passenger   = $passengers[$pk]['origin_passenger'];
        $item_data          = $change_seat_item->toArray();
        $map                = $passenger_type_map[$passenger_type];
        if (!isset($item_data[$map])) {
            throw new Exception("不支持的乘客类型");
        }

        $passenger_type_data = $item_data[$map];
        $price_diff          = $passenger_type_data['price_diff'];
        $origin_ticket_price = $origin_passenger['ticket_price'];
        $curr_ticket_price   = $origin_ticket_price + $price_diff;
//        // TODO for test 票价待确认
//        $passenger_type_data['change_fee'] = -1;
//        // for test
        $price_confirmed = $passenger_type_data['change_fee'] >= 0
                           && $passenger_type_data['upgrade_fee'] >= 0;
        if (!$price_confirmed) {
            $total_price = -1;
        } else {
            $total_price = $passenger_type_data['change_fee']
                           + $passenger_type_data['upgrade_fee']
                           + $price_diff
                           + $passenger_type_data['construction_fee_diff']
                           + $passenger_type_data['fuel_fee_diff'];
        }

        $passengers[$pk]['price_details']         = [];
        $passengers[$pk]['total_price']           = $total_price;                                                                        // 改期服务费
        $passengers[$pk]['change_fee']            = $passenger_type_data['change_fee'];                                                  // 改签服务费
        $passengers[$pk]['upgrade_fee']           = $passenger_type_data['upgrade_fee'];                                                 // 升舱费
        $passengers[$pk]['ticket_price_diff']     = $price_diff;                                                                         // 机票差价
        $passengers[$pk]['ticket_price']          = $curr_ticket_price;                                                                  // 用差价 + 原票价 = 当前价格
        $passengers[$pk]['construct_fee']         = $passenger_type_data['construction_fee'];                                            //
        $passengers[$pk]['fuel_fee']              = $passenger_type_data['fuel_fee'];                                                    //
        $passengers[$pk]['construction_fee_diff'] = $passenger_type_data['construction_fee_diff'];                                       //
        $passengers[$pk]['fuel_fee_diff']         = $passenger_type_data['fuel_fee_diff'];                                               //
        $passengers[$pk]['service_fee']           = 0;                                                                                   // 服务费
        $passengers[$pk]['cabin']                 = $item_data['cabin'];                                                                 // 新舱位
        return $passengers;
    }
}